﻿using System.Collections.Generic;
using System.Linq;
using System;

namespace CRMS.Models
{
    public class OpenCourseInfoRep
    {
        private StoreDBDataContext storeDB = new StoreDBDataContext();

        #region 几个用于添加教师开课信息的内部类及获取的方法
        public class classroom
        {
            public string roomName { get; set; }
            public int roomID { get; set; }
            public classroom(string name, int id)
            {
                roomName = name;
                roomID = id;
            }
        }
        public class course
        {
            public string cName { get; set; }
            public string cID { get; set; }
            public course(string name,string id)
            {
                cName = name;
                cID = id;
            }

        }
        public class teacher
        {
            public string tName { get; set; }
            public string tID { get; set; }
            public teacher(string name, string id)
            {
                tName = name;
                tID = id;
            }
        }
        

        public IQueryable<classroom> getRooms(string bid)
        {
            return from r in storeDB.Classrooms
                   where r.TeachingBuilding.TeachingBuildingId == bid 
                   select new classroom(r.Floor, r.ClassroomId);
        }
        public IQueryable<course> getCourses()
        {
            return from c in storeDB.Courses
                   select new course(c.CourseName,c.CourseId);
        }
        public IQueryable<teacher> getTeachers()
        {
            return from t in storeDB.Teachers
                   select new teacher(t.TeacherName,t.TeacherId);
        }
        #endregion
        //
        //Query methods
        #region 获取所有老师的所有开课信息

        /// <summary>
        /// 获取所有老师的所有开课信息
        /// </summary>
        /// <returns>返回所有的开课信息</returns>
        public IQueryable<OpenCourseInfo> getAllOpenCourseInfos()
        {
            return from o in
                       (from t in storeDB.Teachers
                        join tc in storeDB.TeacherCourses
                        on t.TeacherId equals tc.TeacherId
                        select new { t, tc })
                   join c in storeDB.Courses
                   on o.tc.CourseId equals c.CourseId
                   select new OpenCourseInfo(o.t, o.tc, c);
        }
        #endregion

        #region 通过教师的编号，查询教师的开课信息
        /// <summary>
        /// 通过教师的编号，查询教师的开课信息
        /// </summary>
        /// <param name="teacherId">教师编号参数</param>
        /// <returns>返回该教师的开课信息</returns>
        public IQueryable<OpenCourseInfo> getOpenCourseInfo(string teacherId)
        {
            return from o in
                       (from t in storeDB.Teachers
                        join tc in storeDB.TeacherCourses
                        on t.TeacherId equals tc.TeacherId
                        where t.TeacherId == teacherId
                        select new { t, tc })
                   join c in storeDB.Courses
                   on o.tc.CourseId equals c.CourseId
                   select new OpenCourseInfo(o.t, o.tc, c);
        }
        #endregion

        #region 通过教师的名字，查询教师的所以开课信息
        /// <summary>
        /// 通过教师的编号，查询教师的开课信息
        /// </summary>
        /// <param name="teacherId">教师姓名参数</param>
        /// <returns>返回该教师的开课信息</returns>
        public IQueryable<OpenCourseInfo> getOpenCourseInfoByTName(string teacherName)
        {
            return from o in
                       (from t in storeDB.Teachers
                        join tc in storeDB.TeacherCourses
                        on t.TeacherId equals tc.TeacherId
                        where t.TeacherName == teacherName
                        select new { t, tc })
                   join c in storeDB.Courses
                   on o.tc.CourseId equals c.CourseId
                   select new OpenCourseInfo(o.t, o.tc, c);
        }
        #endregion

        /// <summary>
        /// 通过开课信息主键获取开课信息
        /// </summary>
        /// <param name="cid">课程编号</param>
        /// <param name="tid">教师编号</param>
        /// <returns>返回开课信息</returns>
        public TeacherCourse getOpenCourse(string cid,string tid)
        {
            return storeDB.TeacherCourses.SingleOrDefault(tc => tc.CourseId == cid && tc.TeacherId == tid);
        }

        public bool HadApplyThisRoom(string bid, int cid, string uid, DateTime dt, string w, string p)
        {
            ToUseClassroom useRoom = storeDB.ToUseClassrooms.SingleOrDefault(t => t.TeachingBuildingId == bid
                && t.ClassroomId == cid && t.UserId == uid && t.UseDate == dt && t.Weekday == w && t.Period == p);
            if (useRoom == null)
                return false;
            else
                return true;
        }

        public void Add(TeacherCourse tc)
        {
            storeDB.TeacherCourses.InsertOnSubmit(tc);
        }
        public void Add(ToUseClassroom tuc)
        {
            storeDB.ToUseClassrooms.InsertOnSubmit(tuc);
        }
        public void Delete(TeacherCourse tc)
        {
            storeDB.TeacherCourses.DeleteOnSubmit(tc);
        }

        public void Save()
        {
            storeDB.SubmitChanges();
        }
    }
}
